package com.ykdl.member.kid.adapters;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.RelativeLayout.LayoutParams;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.beans.AskDoctorBean;
import com.ykdl.member.kid.doctor.DoctorInfoActivity;
import com.ykdl.member.kid.doctor.DoctorListAcivity;
import com.ykdl.member.kid.doctor.DoctorOnlineActivity;
import com.ykdl.member.kid.image.ImageOptionsFactory;
import com.ykdl.member.kid.util.DateUtil;
import com.ykdl.member.kid.widget.AsyncImageView;

public class AskDoctorAdapter extends BaseAdapter implements OnClickListener {
	ArrayList<AskDoctorBean>  list = null;
	private Activity mContext ;
	private String TAG = "AskDoctorActivity";
	private boolean isShow ;
	private BitmapUtils bitmapUtil ;
	public AskDoctorAdapter(Activity context,boolean isShow){
		this.mContext = context;
		this.isShow = isShow ;
		bitmapUtil = new BitmapUtils(context);
		bitmapUtil.configDefaultLoadingImage(R.drawable.defualt_doctor_icon);
		bitmapUtil.configDefaultLoadFailedImage(R.drawable.defualt_doctor_icon);
	}
	public void setList(ArrayList<AskDoctorBean>  list) {
		this.list = list;
	}

	public ArrayList<AskDoctorBean>getList() {
		return list;
	}
	@Override
	public int getCount() {
		if (list != null) {
			return list.size();
		} else {
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		if (getCount() != 0) {
			return list.get(position-1);
		} else {
			return null;
		}
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 3;
	}

	@Override
	public int getItemViewType(int position) {
		AskDoctorBean bean = (AskDoctorBean) list.get(position);
		if (bean.getAnswer()==null) {
			return 1;
		} else {
			return 2;
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder1 holder1 = null;
		ViewHolder2 holder2 = null;
		int type = getItemViewType(position);
		if (convertView == null) {
			if (type == 1) {
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.iask2_doc_item1, null);
				holder1 = new ViewHolder1();
				setView1(holder1, convertView);
				convertView.setTag(holder1);
			} else {
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.iask2_doc_item2, null);
				holder2 = new ViewHolder2();
				setView2(holder2, convertView);
				convertView.setTag(holder2);
			}
		} else {
			if (type == 1) {
				holder1 = (ViewHolder1) convertView.getTag();
			} else {
				holder2 = (ViewHolder2) convertView.getTag();
			}
		}
		if (type == 1) {// 医生发布的话题
			holder1.item_content.setText(list.get(position).getTitle());
			if (null!=list.get(position).getExtra_data()) {
				if (null!=list.get(position).getExtra_data().getBaby_days()) {
					switch (list.get(position).getExtra_data().getStatus()) {
					case 1:
						holder1.item_age.setText(DateUtil.getDue_date(list.get(position).getExtra_data().getBaby_days()));
						break;
					case 2:
						holder1.item_age.setText(DateUtil.getDayTip(list.get(position).getExtra_data().getBaby_days()));
						break;
					case 3:
						holder1.item_age.setText("6岁+");
						break;

					default:
//						holder1.item_age.setText("");
						break;
					}
				}else {
					holder1.item_age.setText("");
				}
//				 String strAge = DateUtil.getBabyAge((long) list.get(position).getPost_actor().getBaby_profile().getBirthday()*1000, System.currentTimeMillis());
//				holder1.item_age.setText(strAge);
			}else {
				holder1.item_age.setText("");
			}
			
			holder1.item_time.setText(DateUtil.gettopicDate(list.get(position).getPost_time()));
		} else {
			final int index = position;
			holder2.item_question_content.setText(list.get(position).getTitle());
			holder2.item_answer_content.setText(list.get(position).getAnswer().getMessage());
			if (isShow) {
				holder2.head_lp.setVisibility(View.VISIBLE);
				if (list.get(position).getAnswer().getPost_actor().getAvatar_file_meta()!=null) {
					String url = list.get(position).getAnswer().getPost_actor().getAvatar_file_meta().getDownload_urls().getSmal().getUrl();
					bitmapUtil.display(holder2.head, url);
				}else {
					holder2.head.setImageResource(R.drawable.defualt_doctor_icon);
				}
				holder2.head.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(mContext, DoctorOnlineActivity.class);
						intent.putExtra("doctor_id", list.get(index).getAnswer().getPost_actor().getActor_id()+"");
						mContext.startActivity(intent);
					}
				});
			}else {
				LayoutParams  params = (LayoutParams) holder2.head.getLayoutParams();
				params.width =LayoutParams.WRAP_CONTENT;
				params.height = LayoutParams.WRAP_CONTENT ;
				holder2.head.setLayoutParams(params);
				holder2.head.setBackgroundResource(R.drawable.iask_doc_answer);
				holder2.head_lp.setVisibility(View.GONE);
			}
			
			holder2.name.setText(list.get(position).getAnswer().getPost_actor().getDisplay_name()+"");
			if (null!=list.get(position).getExtra_data()) {
				if (null!=list.get(position).getExtra_data().getBaby_days()) {
					switch (list.get(position).getExtra_data().getStatus()) {
					case 1:
						holder2.item_age.setText(DateUtil.getDue_date(list.get(position).getExtra_data().getBaby_days()));
						break;
					case 2:
						holder2.item_age.setText(DateUtil.getDayTip(list.get(position).getExtra_data().getBaby_days()));
						break;
					case 3:
						holder2.item_age.setText("6岁+");
						break;

					default:
						holder2.item_age.setText("");
						break;
					}
				}else {
					holder2.item_age.setText("");
				}
//				if (null!=list.get(position).getPost_actor().getBaby_profile()) {
//					 String strAge = DateUtil.getBabyAge((long) list.get(position).getPost_actor().getBaby_profile().getBirthday()*1000, System.currentTimeMillis());
//					 holder2.item_age.setText(strAge);
//				}
			}else {
				holder2.item_age.setText("");
			}
			
			holder2.item_time.setText(DateUtil.gettopicDate(list.get(position).getPost_time()));
			if (list.get(position).getAnswer().getPost_actor().getProfile()!=null) {
				holder2.doctor_position.setText(list.get(position).getAnswer().getPost_actor().getProfile().getTitle()+"");
			}
			
		}
		return convertView;

	}

	public void setView1(ViewHolder1 holder, View view) {
		holder.item_content = (TextView) view
				.findViewById(R.id.item_question_content);
		holder.item_age = (TextView) view.findViewById(R.id.item_age);
		holder.item_time = (TextView) view.findViewById(R.id.item_time);
		holder.item_time = (TextView) view
				.findViewById(R.id.item_time);
	}

	public void setView2(ViewHolder2 holder, View view) {
		holder.item_question_content = (TextView) view
				.findViewById(R.id.item_question_content);
		holder.item_answer_content = (TextView) view
				.findViewById(R.id.item_answer_content);
		holder.head = (ImageView) view.findViewById(R.id.head);
		holder.head_lp  = (ImageView) view.findViewById(R.id.head_lp);
		holder.head.setOnClickListener(this);
		holder.name = (TextView) view.findViewById(R.id.name);
		holder.item_age = (TextView) view.findViewById(R.id.item_age);
		holder.item_time = (TextView) view.findViewById(R.id.item_time);
		holder.doctor_position = (TextView) view
				.findViewById(R.id.doctor_position);
	}

	@Override
	public void onClick(View v) {
		if (v.getTag() != null) {
		}

	}
	class ViewHolder1 {
		TextView item_content;
		TextView item_age;
		TextView item_time;
	}

	class ViewHolder2 {
		TextView item_question_content;
		TextView item_answer_content;
		ImageView head;
		ImageView head_lp;
		TextView name;
		TextView item_age;
		TextView item_time;
		TextView doctor_position;
	}
}
